package aModelLayer;

import java.util.*;

public class Load {
	private static final String BUNDLE_NAME = "aModelLayer.saveFile";
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
	
	Game mModel;

	public Load(Game model) {
		mModel = model;
		
		Inventory.clearInventory();
		String ref = "";
			
		for(int i = 0; i > 0; i++) {
			ref = Load.getString("Inventory." + i);
			if (ref.equals("!" + i + "!"))
				i = -1;
			else
				Inventory.addItem(new Item(Integer.parseInt(ref)));
		}
		
		BattleCharacter[] battleCharacters = model.getBattleCharacters();
		
		for (int i = 0; i < 3; i++) {
			battleCharacters[i].setCurrentHealth(Integer.parseInt(Load.getString("BattleCharacter." + i + "0")));
			battleCharacters[i].setCurrentMana(Integer.parseInt(Load.getString("BattleCharacter." + i + "1")));
			battleCharacters[i].setMaxHealth(Integer.parseInt(Load.getString("BattleCharacter." + i + "2")));
			battleCharacters[i].setMaxMana(Integer.parseInt(Load.getString("BattleCharacter." + i + "3")));
			battleCharacters[i].setStrength(Integer.parseInt(Load.getString("BattleCharacter." + i + "4")));
			battleCharacters[i].setDefense(Integer.parseInt(Load.getString("BattleCharacter." + i + "5")));
			battleCharacters[i].setSpeed(Integer.parseInt(Load.getString("BattleCharacter." + i + "6")));
			battleCharacters[i].setMagic(Integer.parseInt(Load.getString("BattleCharacter." + i + "7")));
				
			if(Integer.parseInt(Load.getString("BattleCharacter." + i + "8")) != 0)
				battleCharacters[i].equipHead(Integer.parseInt(Load.getString("BattleCharacter." + i + "8")));
			else
				if(battleCharacters[i].getHeadEquip().isItem())
					battleCharacters[i].unequipHead();
			if(Integer.parseInt(Load.getString("BattleCharacter." + i + "9")) != 0)
					battleCharacters[i].equipChest(Integer.parseInt(Load.getString("BattleCharacter." + i + "9")));
			else
				if(battleCharacters[i].getChestEquip().isItem())
					battleCharacters[i].unequipChest();
			if(Integer.parseInt(Load.getString("BattleCharacter." + i + "10")) != 0)
				battleCharacters[i].equipWeapon(Integer.parseInt(Load.getString("BattleCharacter." + i + "10")));
			else
				if(battleCharacters[i].getWeaponEquip().isItem())
					battleCharacters[i].unequipWeapon();
			
			battleCharacters[i].setXP(Integer.parseInt(Load.getString("BattleCharacter." + i + "11")));
			battleCharacters[i].setLevel(Integer.parseInt(Load.getString("BattleCharacter." + i + "12")));
		}
	}

	public static String getString(String key) {
		try {
			return RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
	}
}
